package com.google.android.DemoKit;

import java.util.List;

import com.hethongviet.vending.AppConfig;
import com.hethongviet.vending.AppMain;
import com.hethongviet.vending.Constant;
import com.hethongviet.vending.helper.db.AccountSQLite;
import com.hethongviet.vending.helper.db.BoxSQLite;
import com.hethongviet.vending.helper.db.MachineSQLite;
import com.hethongviet.vending.helper.db.ProductSQLite;
import com.hethongviet.vending.model.Account;
import com.hethongviet.vending.model.Box;
import com.hethongviet.vending.model.MachineInfo;
import com.hethongviet.vending.model.Product;
import com.hethongviet.vending.network.CONFIG;
import com.hethongviet.vending.network.Network;
import com.hethongviet.vending.pages.StandbyActivity;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

/** Using for USB connection */
public class DemoKitLaunch extends Activity
{
	static final String TAG = "DemoKitLaunch";

	protected DemoKitActivity mHostActivity;

	private boolean _isFirstRun = true;

	private MachineInfo _machine = null;

	private AccountSQLite _accountSqLite = null;
	private BoxSQLite _boxSqLite = null;
	private ProductSQLite _productSqLite = null;
	private MachineSQLite _machineSqLite = null;

	public DemoKitLaunch()
	{
		_accountSqLite = new AccountSQLite(this);
		_boxSqLite = new BoxSQLite(this);
		_productSqLite = new ProductSQLite(this);
		_machineSqLite = new MachineSQLite(this);
	}

	static Intent createIntent(Activity activity)
	{
		Intent intent;

		{
			Log.i(TAG, "starting phone ui");
			intent = new Intent(activity, StandbyActivity.class);
		}
		return intent;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		initDatabase();

		if (_isFirstRun)
		{
			initData();
		}

		_machine = AppMain.getInstance().getMachineInfo();

		Network.setPassword(_machine.getPasswordNetwork());
		Network.setUsername(_machine.getUsernameNetwork());

		Intent intent = createIntent(this);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
				| Intent.FLAG_ACTIVITY_CLEAR_TOP);

		try
		{
			startActivity(intent);
		} catch (ActivityNotFoundException e)
		{
			Log.e(TAG, "unable to start DemoKit activity", e);
		}
		finish();
	}

	private void initDatabase()
	{
		/* Create all table */
		_accountSqLite.open();
		_accountSqLite.close();

		/* This is the first time? */
		List<Box> boxsList = null;
		try
		{
			boxsList = _boxSqLite.getAll();
		} catch (Exception e)
		{
			e.printStackTrace();
			boxsList = null;
		}

		if (boxsList == null || boxsList.size() == 0)
			_isFirstRun = true;
		else
			_isFirstRun = false;
	}

	private void initData()
	{

		// if (!_accountSqLite.isTableExist())
		{
			_accountSqLite.deleteAll();

			_accountSqLite.insert(new Account("0937813357", "Dinh Thi Duyen",
					0, 0, Account.PERMIT_USER, false, "", true,
					System.currentTimeMillis(), "", "duyen", "duyen", 0));

			_accountSqLite.insert(new Account("0942478466", "Dinh Ngoc Tan", 0,
					0, Account.PERMIT_USER, false, "", true,
					System.currentTimeMillis(), "", "bkit05", "ngoctan", 0));

			_accountSqLite.insert(new Account("0975992243", "Dinh Thi Duyen",
					0, 0, Account.PERMIT_USER, false, "", true,
					System.currentTimeMillis(), "", "duyen", "duyen", 0));

			_accountSqLite.insert(new Account("0903946510",
					"Nguyen Chien Thang", 0, 0, Account.PERMIT_ADMIN, false,
					"", true, System.currentTimeMillis(), "", "thang", "thang",
					0));

		}

		// if (!AppMain.getInstance()._machineInfoSqLite.isTableExist())
		{
			_machineSqLite.deleteAll();

			// HTC One V
			MachineInfo machine;
			machine = new MachineInfo("VM002", 40, System.currentTimeMillis(),
					0, 15, 342142142.23423, 0, 234242.23423, "0975075350", "",
					"0975075350", 1, 1, "0975075350", "1C:B0:94:6E:B8:93",
					"00:12:03:09:14:98", "http://localhost", 1234);

			// Galaxy tab
			machine = new MachineInfo("VM002", 40, System.currentTimeMillis(),
					0, 15, 342142142.23423, 0, 234242.23423, "0975075350", "",
					"0975075350", 1, 1, "0975075350", "CC:F9:E8:1A:DB:23",
					"00:12:03:09:14:98", "http://localhost", 1234);

			// LG P350
			machine = new MachineInfo("VM002", 40, System.currentTimeMillis(),
					0, 15, 342142142.23423, 0, 234242.23423, "0975075350", "",
					"0975075350", 1, 1, "0975075350", "5C:17:D3:9B:2E:76",
					"00:12:03:09:14:98", "http://localhost", 1234);

			machine.setUsernameNetwork(CONFIG.NETWORK_USERNAME);
			machine.setPasswordNetwork(CONFIG.NETWORK_PASSWORD);
			_machineSqLite.insert(machine);
		}

		// if (!_productSqLite.isTableExist())
		{
			_productSqLite.deleteAll();

			if (AppConfig.USE_ACCOUNT_MONEY)
			{
				_productSqLite.insert(new Product("PEPSI", "K0", 0, 5000,
						"Hang chat luong cao", "index.png"));
				_productSqLite.insert(new Product("AQUAFINA", "K1", 0, 5000,
						"Hang chat luong cao", "index.png"));
				_productSqLite.insert(new Product("STING", "K2", 0, 8000,
						"Hang chat luong cao", "index.png"));
				_productSqLite.insert(new Product("YELLOW STING", "K3", 0,
						9000, "Hang chat luong cao", "index.png"));
				_productSqLite.insert(new Product("SEVENUP", "K4", 0, 8000,
						"Hang chat luong cao", "index.png"));
				_productSqLite.insert(new Product("REVIVE", "K5", 0, 8000,
						"Hang chat luong cao", "index.png"));
				_productSqLite.insert(new Product("TWISTER", "K6", 0, 9000,
						"Hang chat luong cao", "index.png"));
				_productSqLite.insert(new Product("MIRINDA CAM", "K7", 0, 8000,
						"Hang chat luong cao", "index.png"));
				_productSqLite.insert(new Product("MIRINDA XA XI", "K8", 0,
						9000, "Hang chat luong cao", "index.png"));
				_productSqLite.insert(new Product("LIPTON", "K9", 0, 5000,
						"Hang chat luong cao", "index.png"));
			}
			else
			{
				_productSqLite.insert(new Product("52153", "52153", 0, 5000,
						"Endmill 3.0mm, 2Fl, TiAlN Coated", "M.A. FORD"));
				_productSqLite.insert(new Product("52164", "52164", 0, 5000,
						"Endmill 6.0mm, 2Fl, TiAlN Coated", "M.A. FORD"));
				_productSqLite.insert(new Product("52170", "52170", 0, 5000,
						"Endmill 8.0mm, 2Fl, TiAlN Coated", "M.A. FORD"));
				_productSqLite.insert(new Product("52174", "52174", 0, 5000,
						"Endmill 10.0mm, 2Fl, TiAlN Coated", "M.A. FORD"));
				_productSqLite.insert(new Product("52178", "52178", 0, 5000,
						"Endmill 12.0mm, 2Fl, TiAlN Coated", "M.A. FORD"));

				_productSqLite.insert(new Product("CNMG120408FWKC9110",
						"CNMG120408FWKC9110", 0, 5000,
						"Turning insert Wiper, Steel, R0.8", "KENNAMETAL"));
				_productSqLite.insert(new Product("CNMG120408MNKC9110",
						"CNMG120408MNKC9110", 0, 5000,
						"Turning insert Medium, Steel, R0.8", "KENNAMETAL"));
				_productSqLite.insert(new Product("CNMG120408MNKC9125",
						"CNMG120408MNKC9125", 0, 5000,
						"Turning insert Medium, Steel, R0.8", "KENNAMETAL"));
				_productSqLite.insert(new Product("CNMG120408MNKC9140",
						"CNMG120408MNKC9140", 0, 5000,
						"Turning insert Medium, Steel, R0.8", "KENNAMETAL"));

				_productSqLite.insert(new Product("CNMG120408MSKC5525",
						"CNMG120408MSKC5525", 0, 5000,
						"Turning insert Medium, Super alloy, R0.8",
						"KENNAMETAL"));

				_productSqLite.insert(new Product("CNMG120408RPKC5010",
						"CNMG120408RPKC5010", 0, 5000, "Hang chat luong cao",
						"KENNAMETAL"));

				_productSqLite.insert(new Product("CNMG120412FNKC9110",
						"CNMG120412FNKC9110", 0, 5000,
						"Turining insert Finishing, Steel, R1.2", "KENNAMETAL"));
				_productSqLite.insert(new Product("CNMG120412FWKC9110",
						"CNMG120412FWKC9110", 0, 5000,
						"Turning Insert Wiper, Steel, R1.2", "KENNAMETAL"));
				_productSqLite.insert(new Product("CNMG120412FWKC9125",
						"CNMG120412FWKC9125", 0, 5000,
						"Turning Insert Wiper, Steel, R1.2", "KENNAMETAL"));

				_productSqLite.insert(new Product("CNMG120412MSK313",
						"CNMG120412MSK313", 0, 5000,
						"Turning Insert Medium, Cermet, R1.2", "KENNAMETAL"));
				_productSqLite.insert(new Product("WNMG080408MNKC9110",
						"WNMG080408MNKC9110", 0, 5000,
						"Turning insert Medium, Steel, R0.8", "KENNAMETAL"));
				_productSqLite.insert(new Product("WNMG080412MNKC9110",
						"WNMG080412MNKC9110", 0, 5000,
						"Turning insert Medium, Steel, R1.2", "KENNAMETAL"));

				_productSqLite.insert(new Product("CNMG120408MPKC5010",
						"CNMG120408MPKC5010", 0, 5000,
						"Turning insert Medium, Steel, R0.8", "KENNAMETAL"));
				_productSqLite.insert(new Product("CNMG120408MPKCM25",
						"CNMG120408MPKCM25", 0, 5000,
						"Turning insert Medium, Steel, R0.8", "KENNAMETAL"));
				_productSqLite.insert(new Product("CNMG120408MPKCP25",
						"CNMG120408MPKCP25", 0, 5000,
						"Turning insert Medium, Steel, R1.2", "KENNAMETAL"));

				_productSqLite.insert(new Product("CNMG120412MPKCM25",
						"CNMG120412MPKCM25", 0, 5000,
						"Turning insert Medium, Steel, R0.8", "KENNAMETAL"));

				_productSqLite.insert(new Product("CNMG160408FNKCP25",
						"CNMG160408FNKCP25", 0, 5000,
						"Turning insert Medium, Steel, R0.8", "KENNAMETAL"));

				_productSqLite.insert(new Product("CNMG160608MNKC9110",
						"CNMG160608MNKC9110", 0, 5000,
						"Turning insert Medium, Steel, R0.8", "KENNAMETAL"));

				_productSqLite.insert(new Product("CNMG160612MPKC5010",
						"CNMG160612MPKC5010", 0, 5000,
						"Turning insert Medium, Steel, R1.2", "KENNAMETAL"));
				_productSqLite.insert(new Product("TCMT16T308LFKC5010",
						"TCMT16T308LFKC5010", 0, 5000,
						"Turning insert Medium, Steel, R0.8", "KENNAMETAL"));

				_productSqLite.insert(new Product("CNMG160608RPKCP25",
						"CNMG160608RPKCP25", 0, 5000,
						"Turning insert Medium, Steel, R0.8", "KENNAMETAL"));
				_productSqLite.insert(new Product("CNMG160608MPKCM25",
						"CNMG160608MPKCM25", 0, 5000,
						"Turning insert Medium, Steel, R0.8", "KENNAMETAL"));
				_productSqLite.insert(new Product("SPGN090304KC5010",
						"SPGN090304KC5010", 0, 5000,
						"Turning insert Medium, Steel, R1.2", "KENNAMETAL"));
				_productSqLite.insert(new Product("TCMT16T303LFKC5025",
						"TCMT16T303LFKC5025", 0, 5000,
						"Turning insert Medium, Steel, R1.2", "KENNAMETAL"));

				_productSqLite.insert(new Product("Vien Bo mau", "DEERVET", 0,
						5000, "", "KENNAMETAL"));
				_productSqLite.insert(new Product("DNMG150404MNKC9125",
						"DNMG150404MNKC9125", 0, 5000, "", "KENNAMETAL"));

				_productSqLite.insert(new Product("TPGT090204LFKC5410",
						"TPGT090204LFKC5410", 0, 5000, "", "KENNAMETAL"));
				_productSqLite.insert(new Product("TPGT110204LFKC5410",
						"TPGT110204LFKC5410", 0, 5000, "", "KENNAMETAL"));
				_productSqLite.insert(new Product("TPUN110304K68",
						"TPUN110304K68", 0, 5000, "", "KENNAMETAL"));
				_productSqLite.insert(new Product("VBMT160408LFKC9110",
						"VBMT160408LFKC9110", 0, 5000, "", "KENNAMETAL"));
				_productSqLite.insert(new Product("VBMT160408LFKC9125",
						"VBMT160408LFKC9125", 0, 5000, "", "KENNAMETAL"));
			}
		}
		// if (!_boxSqLite.isTableExist())
		{
			_boxSqLite.deleteAll();

			for (int index = 0; index < Constant.MAX_BOX_AMOUNT - 5; index++)
			{
				_boxSqLite.insert(new Box(index + 1, String.format("%02d",
						index), "", 0, 0, 0, "", 1));
			}
		}

	}

}
